using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Diagnostics;

namespace IPConsole
{
    class Program
    {
        private const int BAUD = 115200;
        private static int NUMLoops = 10;
        public static bool IsLoopback = false;

        private static System.IO.Ports.StreamDriverPort StreamDriverPort1;

        private static int count;
        private static int cntr;
        private static bool err = false;
        static void Main(string[] args)
        {

            IsLoopback = false;

            Debug.WriteLine("Welcome to C# on Windows Embedded Systems: Get IP Address");
            StreamDriverPort1 = new System.IO.Ports.StreamDriverPort();
            StreamDriverPort1.DataBits = 8;
            StreamDriverPort1.Handshake = System.IO.Ports.Handshake.None;
            StreamDriverPort1.Parity = System.IO.Ports.Parity.None;
            StreamDriverPort1.StopBits = System.IO.Ports.StopBits.One;
            StreamDriverPort1.BaudRate = BAUD;

            count = 0;
            int cntr = NUMLoops;
            while (cntr-- != 0)
            {
                DoIP();
                if (err)
                    return;
                System.Threading.Thread.Sleep(1000);
            }
        }

        private static void DoIP()
        {

            string host = Dns.GetHostName();
            IPHostEntry ip = Dns.GetHostEntry(host);

            //In case this list changes, eg Plugin USB NIC
            if (count > ip.AddressList.Count() - 1)
                count = 0;
            //Get next index, downlwards
            count--;
            //Restart if last did [0]
            if (count == -1)
                count = ip.AddressList.Count() - 1;
            //Address zero is IPv6, address 1 is ipv4, hence start from end of list
            IPAddress ipaddr = ip.AddressList[count];
            if (ipaddr.AddressFamily == AddressFamily.InterNetwork)
            {
                //Make sure do 20 loops after get Loopback (first)
                if (ipaddr.ToString() == "127.0.0.1")
                {
                    if (!IsLoopback)
                    {
                        cntr = 20;
                        System.Threading.Thread.Sleep(2000);
                        IsLoopback = true;
                    }
                }
                else
                {
                    //Do 8 loops after get non loopback
                    if (IsLoopback)
                    {
                        cntr = 6;
                        IsLoopback = false;
                    }
                }
            }

            //Write to label
            //label1.Text = ipaddr;

            //Send IPAddress to COM1
            if (!StreamDriverPort1.IsOpen)
                try
                {
                    StreamDriverPort1.Open();
                }
                catch (Exception ex)
                {
                    err = true;
                }
            if (StreamDriverPort1.IsOpen)
            {
                string message = "\r\n IPAddress = " + ipaddr.ToString() + "\r\n";
                StreamDriverPort1.WriteLine(message);
                Debug.WriteLine(message);
            }
        }
    }
}

























